package apis

import (
	"encoding/json"
	"gitee.com/sansaniot/ssiot-core/httpmvc/api"
	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"gitee.com/sansaniot/ssiot-core/serrors"
	"github.com/gin-gonic/gin"
	global "ssadmin/common/constant"
	"ssadmin/common/utils"
	"ssadmin/internal/admin/models"
	"ssadmin/internal/admin/models/command"
	"ssadmin/internal/admin/models/query"
	"ssadmin/internal/admin/service"
)

type Menu struct {
	api.Api
}

func (e Menu) GetSystemMenu(c *gin.Context) {
	req := query.MenuQuery{}
	//查询系统菜单，需要排除按钮
	req.Type = "1"
	e.getMenuList(c, &req)
}

func (e Menu) GetList(c *gin.Context) {
	req := query.MenuQuery{}
	e.getMenuList(c, &req)
}

func (e Menu) getMenuList(c *gin.Context, req *query.MenuQuery) {
	//检查systemid
	req.Systemid = c.GetHeader(global.CacheKeySystemId)
	if len(req.Systemid) == 0 {
		e.MakeContext(c)
		e.Fail(serrors.Fail)
		return
	}

	s := service.Menu{}
	err := e.MakeContext(c).MakeOrm().
		Bind(req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	var list []models.Menu
	req.UserId = user.Get(c, global.CacheKeyUserId).(string)
	err = s.GetBySystem(req, &list)
	data, _ := json.Marshal(&list)
	dataMap := make([]map[string]interface{}, 0)
	_ = json.Unmarshal(data, &dataMap)
	tree := utils.ConvrtListToTree(dataMap, "id")

	resData := make(map[string]interface{})
	root := make(map[string]interface{})
	root["hasChildren"] = true
	root["hasParent"] = false
	root["id"] = "0"
	root["parentId"] = ""
	root["text"] = "root"
	root["children"] = tree

	resData["records"] = root
	e.Data(resData)
}

func (e Menu) EditOrder(c *gin.Context) {
	s := service.Menu{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	var req []command.MenuOrderReq
	if err = json.NewDecoder(c.Request.Body).Decode(&req); err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	err = s.EditOrder(req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Ok(nil)
}

// Insert 创建Menu
func (e Menu) Insert(c *gin.Context) {
	req := command.MenuInsertReq{}
	s := service.Menu{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	req.Bind(c)
	err = s.Insert(&req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	e.Data(req.GetId())
}

// Update 修改Menu
func (e Menu) Update(c *gin.Context) {
	req := command.MenuUpdateReq{}
	s := service.Menu{}

	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	req.LoadUser(c)
	err = s.Update(&req)
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail, err.Error())
		return
	}

	e.Data(req.GetId())
}

// Delete 删除Menu
func (e Menu) Delete(c *gin.Context) {
	s := service.Menu{}
	req := command.MenuDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	e.Data(req.GetId())
}
